<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Product
 * @package App\Models
 * @property ProductInventory inventory
 * @property ProductCustoms customs
 * @property Customer $customer
 * @property Collection $consumable
 * @author luffyzhao@vip.126.com
 */
class Product extends Model
{
    protected $fillable = [
        'code', 'customer_id', 'title', 'gross_weight', 'brand'
    ];

    protected static function boot()
    {
        static::addGlobalScope('orderBy', function (Builder $query){
            $query->orderByDesc('id');
        });
        parent::boot();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function customer(){
        return $this->belongsTo(Customer::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function customs(){
        return $this->hasOne(ProductCustoms::class, 'product_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function logs(){
        return $this->hasMany(ProductLog::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function inventory(){
        return $this->hasOne(ProductInventory::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function batchs(){
        return $this->hasMany(BatchInventory::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     * @author luffyzhao@vip.126.com
     */
    public function consumable(){
        return $this->belongsToMany(Consumable::class, 'product_consumable');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function orders(){
        return $this->hasMany(OrderProduct::class);
    }
}
